/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.seedlistgenerator;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
 * @author Jonas
 */
public class XMLBuilder {

    public void buildSeedList(List<MS2Identifications> identificationsList, File xmlFile) {

        try {

            Element featureMap = new Element("featureMap");
            Document doc = new Document(featureMap);

            Element featureList = new Element("featureList");
            // featureCount holds the amount of ms_lims hits in the identificationsList
            String featureCount = "" + identificationsList.size();
            featureList.setAttribute(new Attribute("count", featureCount));

            doc.getRootElement().addContent(featureList);
            int counter = 0;

            for (MS2Identifications identification : identificationsList) {

                String identificationMZ = "" + identification.getPrecursorMZ();
                String identificationRTSec = "" + identification.getRtSec();

                // Build up the XML document in a way that openMS can handle it
                Element feature = new Element("feature");
                feature.setAttribute(new Attribute("id", "f_" + counter));
                counter++;
                feature.addContent(new Element("position").setAttribute("dim", "0").setText(identificationRTSec));
                feature.addContent(new Element("position").setAttribute("dim", "1").setText(identificationMZ));
                feature.addContent(new Element("intensity").setText("0"));
                feature.addContent(new Element("quality").setAttribute("dim", "0").setText("0"));
                feature.addContent(new Element("quality").setAttribute("dim", "1").setText("0"));
                feature.addContent(new Element("overallquality").setText("0"));
                feature.addContent(new Element("charge").setText("0"));

                featureList.addContent(feature);
            }
            
            // Write the XML file to the folder specified
            XMLOutputter xmlOutputter = new XMLOutputter();
            xmlOutputter.setFormat(Format.getPrettyFormat());
            xmlOutputter.output(doc, new FileWriter(xmlFile.toString()));

            System.out.println("File Saved!");


        } catch (IOException io) {
            System.out.println(io.getMessage());
        }

    }
}
